package com.example.petplatform.controller;

import com.example.petplatform.common.Result;
import com.example.petplatform.entity.Param.Params;
import com.example.petplatform.entity.Pet;

import com.example.petplatform.exception.CustomException;
import com.example.petplatform.service.PetService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 宠物领养贴管理接口
 */
@RestController
@RequestMapping("/pet")
public class PetController {
    @Resource
    private PetService petService;

    //获取所有宠物领养信息
    @GetMapping("/getAllPets")
    public Result getAllPets() {
        List<Pet> list = petService.getAllPets();
        return Result.success(list);
    }
    //分页查询宠物领养信息
    @GetMapping("/petSearch")
    public Result petSearch(Params params) {
        PageInfo<Pet> list = petService.petSearch(params);
        return Result.success(list);
    }

    //添加或修改宠物领养信息
    @PostMapping("/petAdd")
    public Result petAdd(@RequestBody Pet pet) {
        if(pet.getId() == null) {
            // 添加数据
            List<Pet> list = petService.petAdd(pet);
            return Result.success(list);
        } else {
            // 修改数据
            List<Pet> list = petService.petUpdate(pet);
            return Result.success(list);
        }
    }

    //删除宠物领养信息
    @DeleteMapping("/petDelete") 
    public Result petDelete(@RequestBody Pet pet) {
        List<Pet> list = petService.petDelete(pet.getId());
        return Result.success(list);
    }

    /**
     * 获取宠物详情
     */
    @GetMapping("/detail/{id}")
    public Result getPetDetail(@PathVariable Long id) {
        try {
            Pet pet = petService.getPetDetail(id);
            if (pet != null) {
                return Result.success(pet);
            }
            throw new CustomException("宠物不存在");
        } catch (Exception e) {
            throw new CustomException("获取宠物详情失败: " + e.getMessage());
        }
    }
}
